$(function () {
    /*     var loading = weui.loading('loading', {
            className: 'custom-classname'
        }); */
    location.href = "#bottom";
    var r_userid = Common.fn.getUrlParam('userid'),
        nickname = Common.fn.getUrlParam('nickname');

    var result1 = Model.user.findUserByUserid({
        userid: r_userid
    });
    nickname = r_userid;
    // if (!cid) {
    //     weui.alert('用户参数错误');
    //     location.href = 'message.html';
    // }
    var result = Model.message.messageDetail({
        r_userid: r_userid
    });
    // if (!sessionStorage.getItem('hmdj_stopError')) {
    //     sessionStorage.setItem('hmdj_stopError', true);
    // } else {
    //     if (result.code) {
    //         Common.fn.formatOutput(result);
    //         loading.hide();
    //         return false;
    //     }
    // }
    // 信息
    $('.nickname').text(result1.loginedUser.nickname);
    // if (!result.data[0].business.nickanme) {
    //     $('.nickname').text(nickname);
    // }
    /*
     左侧 接收别人信息 调用，三个参数：leftBubble，头像路径，内容
     chat(element,imgSrc,doctextContent)
     */
    for (var i = 0; i < result.data.length; i++) {
        var messages = result.data[i];
        messages.photo = '../img/user-head-image.jpg';
        // 如果是对方发送信息
        if (messages.s_userid == r_userid) {
            chat("leftBubble", messages.photo, messages.content, messages.id);
        } else {
            // 自己发送的信息
            chat("rightBubble", messages.photo, messages.content, messages.id, 1);
        }
    }


    $(".send-btn").click(function () {
        //清空输入框
        var result = Model.message.putMessage({
            r_userid: r_userid,
            content: $.trim($('.chat-info').html())
        });
        // if (result.code) {
        //     Common.fn.formatOutput(result);
        //     return false;
        // }
        location.href = "#bottom";
        location.reload();
        $('.chat-info').html('');
    });


    $('.clearfix').click(function () {
        var id = $(this).attr('data-id');
        var confirmDom = weui.confirm('要撤回此条消息么？', {
            title: '系统提示',
            buttons: [{
                label: '取消',
                type: 'default',
                onClick: function () {
                    console.log('no')
                }
            }, {
                label: '撤回',
                type: 'primary',
                onClick: function () {
                    var result2 = Model.message.deleteMessage({
                        id: id
                    });
                    if (result.code == 1) {
                        weui.toast('撤回成功', {
                            duration: 1500,
                            className: 'custom-classname',
                            callback: function () {
                                location.reload();
                            }
                        });
                    }

                }
            }]
        });


    })
    // loading.hide();
});

function chat(element, imgSrc, doctextContent, id, type) {
    //判断传入的值，是左侧还是右侧
    var $user = element;
    var $doctorHead = imgSrc;
    //获取输入的值
    if (type == 1) {
        var $textContent = doctextContent;
    } else {
        var $textContent = $('.chat-info').html();
    }
    //获取传入的卖家输入的内容
    var $doctextContent = doctextContent;
    //获取容器
    var $box = $('.bubbleDiv');
    var $boxHeght = $box.height();
    var $sectionHeght = $(".chat-box").height();
    var $elvHeght = Math.abs($boxHeght - $sectionHeght);
    //卖家
    if ($user === "leftBubble") {
        $box.append(createdoct(imgSrc, $doctextContent, id)).animate({
            scrollTop: $(document).height()
        }, 300);
    }
    //买家
    else if ($user === "rightBubble") {
        $box.append(createuser($textContent, imgSrc, id)).animate({
            scrollTop: $(document).height()
        }, 300);
    } else {
        console.log("请传入必须的参数");
    }

}

function createdoct(imgSrc, $doctextContent) {
    var $textContent = $doctextContent;
    var $imgSrc = imgSrc;
    var id = id;
    var block;
    if ($textContent == '' || $textContent == 'null') {
        return;
    }
    block = '<div class="bubbleItem" data-id="' + id + '">' +
        '<div class="doctor-head">' +
        '<img src="' + imgSrc + '" alt="doctor"/>' +
        '</div>' +
        '<span class="bubble leftBubble">' + $textContent + '<span class="topLevel"></span></span>' +
        '</div>';

    return block;
};


function createuser($textContent, imgSrc, id) {
    var $textContent = $textContent;
    var $imgSrc = imgSrc;
    var id = id;
    var block;
    if ($textContent == '' || $textContent == 'null') {
        alert('亲！别太着急，先说点什么～');
        return;
    }

    block = '<div class="bubbleItem clearfix" data-id="' + id + '">' +
        '<span class="bubble rightBubble">' + $textContent +
        '<span class="topLevel"></span>' +
        '<img src="' + imgSrc + '" alt="user"/>' +
        '</span></div>';
    return block;
};

// 限制输入文字字数
$(document).on('keydown', '.chat-info', function () {
    var length = $(this).text().length;
    if (length >= 200) {
        $(this).text($(this).text().substr(0, 199));
    }
});